#include <config.h>
#include <types.h>
#include <init.h>
#include "lib_config.h"
#include "SEGGER_RTT.h"
#include "port.h"


#define port_SYSTICK_CLK_BIT    (1UL << 2UL)
#define port_SYSTICK_INT_BIT    (1UL << 1UL)
#define port_SYSTICK_ENABLE_BIT (1UL << 0UL)

int setup_tick_service(void)
{
	SysTick->LOAD = (configSYS_CLOCK / configHZ) - 1UL;
	SysTick->VAL = 0;
    SysTick->CTRL = (port_SYSTICK_CLK_BIT | port_SYSTICK_INT_BIT | port_SYSTICK_ENABLE_BIT);

    return 0;
}
core_initcall(setup_tick_service);

void console_write_bytes(const void *in, size_t n)
{
#ifdef configUSING_SERIAL_DBG
	const u8 *_in = (const u8 *)in;
    while (n--)
	{
		while (UART_GetFlagStatus(UART5, UART_FLAG_TXIDLE) == RESET);
		UART_SendByte(UART5, *_in++);
	}
#endif
#ifdef configUSING_RTT_DBG
	RTT_write(in, n);
#endif
}
void log_wave(uint16_t value1,uint16_t value2,uint16_t value3,uint16_t value4,uint16_t value5,uint16_t value6,uint16_t value7,uint16_t value8)
{
#ifdef configUSING_SERIAL_DBG

	uint8_t ad_wave[20] = {0};
	ad_wave[0] = 0x03;
	ad_wave[1] = 0xFC;
	ad_wave[3] = value1>> 8;
	ad_wave[2] = value1& 0xff;
	ad_wave[5] = value2>>8;
	ad_wave[4] = value2& 0xff;
	ad_wave[7] = value3>>8;
	ad_wave[6] = value3& 0xff;
	ad_wave[9] = value4>>8;
	ad_wave[8] = value4& 0xff;
	ad_wave[11] = value5>>8;
	ad_wave[10] = value5& 0xff;
	ad_wave[13] = value6>>8;
	ad_wave[12] = value6& 0xff;
	ad_wave[15] = value7>>8;
	ad_wave[14] = value7& 0xff;
	ad_wave[17] = value8>>8;
	ad_wave[16] = value8& 0xff;
	ad_wave[18] = 0xFC;
	ad_wave[19] = 0x03;
	console_write_bytes(ad_wave,20);
	console_write_bytes((uint8_t *)"\r\n",2);
#endif
}

void log_hex(const uint8_t *data,uint16_t len)
{
#ifdef configUSING_SERIAL_DBG

//	int i = 0;
//	uint8_t temp[5] = {0};
//	for(i=0;i < len;i++)
//	{
//		sprintf((char *)temp,"%02X ",data[i]);
//		console_write_bytes(temp,3);
//	}
//	console_write_bytes((uint8_t *)"\r\n",2);
	
	console_write_bytes(data,len);
#endif
}

void setup_console(void)
{
#ifdef configUSING_SERIAL_DBG
	/* USART5 */
	#define CONSOLE_GPIO_TX           GPIO_Pin_15
	#define CONSOLE_GPIO_RX           GPIO_Pin_16
	#define CONSOLE_GPIO_AF           GPIO_Func_1
	#define CONSOLE_GPIO              GPIOA

    GPIO_InitStruType y;
    UART_InitStruType uart0;
   // GPIO_RegUnLock();
    y.GPIO_Signal 	 = GPIO_Pin_Signal_Digital;
    y.GPIO_Func      = CONSOLE_GPIO_AF;
    y.GPIO_Direction = GPIO_Dir_Out;
    y.GPIO_PUEN      = GPIO_PUE_Input_Disable;
    y.GPIO_PDEN      = GPIO_PDE_Input_Disable;
    y.GPIO_OD        = GPIO_ODE_Output_Disable;
    y.GPIO_DS        = GPIO_DS_Output_Normal;
    GPIO_Init(CONSOLE_GPIO, CONSOLE_GPIO_TX, &y);
    
    y.GPIO_Direction = GPIO_Dir_In;
    GPIO_Init(CONSOLE_GPIO, CONSOLE_GPIO_RX, &y);
   
		
   // GPIO_RegLock();
		
    uart0.UART_StopBits = UART_StopBits_1;
    uart0.UART_TxMode   = UART_DataMode_8;
    uart0.UART_TxPolar  = UART_Polar_Normal;
    uart0.UART_RxMode   = UART_DataMode_8;
    uart0.UART_RxPolar  = UART_Polar_Normal;
    uart0.UART_BaudRate = 115200;
    uart0.UART_ClockSet = UART_Clock_1;
    UART_Init(UART5, &uart0);


    UART_TBIMConfig(UART5, UART_TRBIM_Byte);
    UART_RBIMConfig(UART5, UART_TRBIM_Byte);
    //UART_ITConfig(UART5, UART_IT_RB, ENABLE);
    NVIC_Init(NVIC_UART5_IRQn, NVIC_Priority_1, ENABLE);
    UART5_TxEnable();
    //UART5_RxEnable();
#endif
#ifdef configUSING_RTT_DBG
	RTT_init();
#endif
}

#ifdef configUSING_SERIAL_DBG
void UART5_IRQHandler(void)
{
#ifdef configUSING_SHELL
    extern void shell_get_chr(char ch);

    if (UART_GetITStatus(UART5, UART_IT_RB))
    {

        while (UART5->IF.Word & UART_FLAG_RB)
        {
            shell_get_chr(UART5->RBR.Byte[0] & 0xff);
        }
    }
#endif 
}
#endif
